package com.bwda.dsrs.filecenter.ftpsdk.impl;

import com.bwda.dsrs.filecenter.ftpsdk.FtpService;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.List;

/**
 * @description:
 * @author: yangjun
 * @create: 2019-07-16 16:03
 */
public abstract class AbstractFtpService implements FtpService {
    protected String ip;
    protected int port;
    protected String username;
    protected String password;
    protected String workingDirectory = "/";
    protected String controlEncoding = "GB2312";
    protected int enterMode = 1;
    protected int one = 1;
    protected int two = 2;
    protected int timeout = 3000;
    protected boolean inited = false;

    public AbstractFtpService() {
    }

    @Override
    public final boolean init(String ip, int port, String username, String password) {
        if (!StringUtils.isBlank(ip) && !StringUtils.isBlank(username) && !StringUtils.isBlank(password)) {
            this.ip = ip;
            this.port = port;
            this.username = username;
            this.password = password;
            this.inited = true;
            return true;
        } else {
            return false;
        }
    }

    @Override
    public final boolean init(String ip, int port, String username, String password, String controlEncoding, int enterMode) {
        if (!StringUtils.isBlank(ip) && !StringUtils.isBlank(username) && !StringUtils.isBlank(password) && !StringUtils.isBlank(controlEncoding)) {
            if (enterMode != one && enterMode != two) {
                return false;
            } else {
                this.ip = ip;
                this.port = port;
                this.username = username;
                this.password = password;
                this.controlEncoding = controlEncoding;
                this.enterMode = enterMode;
                this.inited = true;
                return true;
            }
        } else {
            return false;
        }
    }

    @Override
    public boolean testConnction() {
        boolean result = false;

        try {
            result = this.connection();
            boolean var4 = result;
            return var4;
        } catch (Exception var7) {
            ;
        } finally {
            this.disConnection();
        }

        return result;
    }

    @Override
    public boolean uploadFile(String strLocalFile, String strRemotePath) {
        boolean result = false;
        if (!StringUtils.isBlank(strLocalFile) && !StringUtils.isBlank(strRemotePath)) {
            strLocalFile = this.formatSeparator(strLocalFile);
            strRemotePath = this.formatSeparator(strRemotePath);
            if (!this.checkLocalFile(strLocalFile)) {
                return result;
            } else {
                boolean var6;
                try {
                    if (!this.connection()) {
                        var6 = result;
                        return var6;
                    }

                    if (this.changeWorkingDirectorySub(strRemotePath) || this.createDirectorySub(strRemotePath)) {
                        result = this.uploadFileSub(strLocalFile, strRemotePath);
                        return result;
                    }

                    var6 = result;
                } catch (Exception var9) {
                    return result;
                } finally {
                    this.disConnection();
                }

                return var6;
            }
        } else {
            return result;
        }
    }

    @Override
    public boolean downloadFile(String strRemoteFile, String strLocalFile) {
        boolean result = false;
        if (!StringUtils.isBlank(strRemoteFile) && !StringUtils.isBlank(strLocalFile)) {
            strLocalFile = this.formatSeparator(strLocalFile);
            strRemoteFile = this.formatSeparator(strRemoteFile);

            boolean var6;
            try {
                if (!this.connection()) {
                    var6 = result;
                    return var6;
                }

                if (this.checkRemoteFileSub(strRemoteFile)) {
                    this.createLocalFolder(strLocalFile);
                    result = this.downloadFileSub(strRemoteFile, strLocalFile);
                    return result;
                }

                var6 = result;
            } catch (Exception var9) {
                return result;
            } finally {
                this.disConnection();
            }

            return var6;
        } else {
            return result;
        }
    }

    @Override
    public boolean deleteFile(String strFileName) {
        boolean result = false;
        strFileName = this.formatSeparator(strFileName);
        if (StringUtils.isBlank(strFileName)) {
            return result;
        } else {
            try {
                if (!this.connection()) {
                    boolean var5 = result;
                    return var5;
                }

                result = this.deleteFileSub(strFileName);
            } catch (Exception var8) {
                ;
            } finally {
                this.disConnection();
            }

            return result;
        }
    }

    @Override
    public boolean createDirectory(String strWorkingDirectory) {
        boolean result = false;
        strWorkingDirectory = this.formatSeparator(strWorkingDirectory);
        if (StringUtils.isBlank(strWorkingDirectory)) {
            return result;
        } else {
            try {
                if (!this.connection()) {
                    boolean var5 = result;
                    return var5;
                }

                result = this.createDirectorySub(strWorkingDirectory);
            } catch (Exception var8) {
                ;
            } finally {
                this.disConnection();
            }

            return result;
        }
    }

    @Override
    public boolean removeDirectory(String strWorkingDirectory) {
        boolean result = false;
        if (StringUtils.isBlank(strWorkingDirectory)) {
            return result;
        } else {
            strWorkingDirectory = this.formatSeparator(strWorkingDirectory);

            try {
                if (!this.connection()) {
                    boolean var5 = result;
                    return var5;
                }

                result = this.removeDirectorySub(strWorkingDirectory);
            } catch (Exception var8) {
                ;
            } finally {
                this.disConnection();
            }

            return result;
        }
    }

    /**
     * @param strRemoteSourceFile
     * @param strRemoteTargetFile
     * @return
     */
    @Override
    public boolean moveFile(String strRemoteSourceFile, String strRemoteTargetFile) {
        boolean result = false;
        if (!StringUtils.isBlank(strRemoteSourceFile) && !StringUtils.isBlank(strRemoteTargetFile)) {
            strRemoteSourceFile = this.formatSeparator(strRemoteSourceFile);
            strRemoteTargetFile = this.formatSeparator(strRemoteTargetFile);
            if (!this.connection()) {
                return result;
            } else if (!this.checkRemoteFileSub(strRemoteSourceFile)) {
                return result;
            } else {
                try {
                    this.copyFileSub(strRemoteSourceFile, strRemoteTargetFile);
                    result = this.deleteFileSub(strRemoteSourceFile);
                } catch (Exception var8) {
                    result = false;
                } finally {
                    this.disConnection();
                }

                return result;
            }
        } else {
            return result;
        }
    }

    /**
     * @param strRemoteSourceFile
     * @param strRemoteTargetFile
     * @return
     */
    @Override
    public boolean copyFile(String strRemoteSourceFile, String strRemoteTargetFile) {
        boolean result = false;
        if (!StringUtils.isBlank(strRemoteSourceFile) && !StringUtils.isBlank(strRemoteTargetFile)) {
            strRemoteSourceFile = this.formatSeparator(strRemoteSourceFile);
            strRemoteTargetFile = this.formatSeparator(strRemoteTargetFile);

            boolean var6;
            try {
                if (this.connection()) {
                    if (!this.checkRemoteFileSub(strRemoteSourceFile)) {
                        var6 = result;
                        return var6;
                    }

                    result = this.copyFileSub(strRemoteSourceFile, strRemoteTargetFile);
                    return result;
                }

                var6 = result;
            } catch (Exception var9) {
                return result;
            } finally {
                this.disConnection();
            }

            return var6;
        } else {
            return result;
        }
    }

    /**
     * @param strRemoteOldName
     * @param strRemoteNewName
     * @return
     */
    @Override
    public boolean rename(String strRemoteOldName, String strRemoteNewName) {
        boolean result = false;
        if (!StringUtils.isBlank(strRemoteOldName) && !StringUtils.isBlank(strRemoteNewName)) {
            strRemoteOldName = this.formatSeparator(strRemoteOldName);
            strRemoteNewName = this.formatSeparator(strRemoteNewName);

            boolean var6;
            try {
                if (this.connection()) {
                    if (!this.checkRemoteFileSub(strRemoteOldName)) {
                        var6 = result;
                        return var6;
                    }

                    result = this.renameSub(strRemoteOldName, strRemoteNewName);
                    return result;
                }

                var6 = result;
            } catch (Exception var9) {
                return result;
            } finally {
                this.disConnection();
            }

            return var6;
        } else {
            return result;
        }
    }

    /**
     * @param strRemotePath
     * @return
     */
    @Override
    public List<String> getFileList(String strRemotePath) {
        if (StringUtils.isBlank(strRemotePath)) {
            return null;
        } else {
            List list = null;

            try {
                if (!this.connection()) {
                    List var5 = list;
                    return var5;
                }

                list = this.getFileListSub(strRemotePath);
            } catch (Exception var8) {
                ;
            } finally {
                this.disConnection();
            }

            return list;
        }
    }

    /**
     * @param strLocalFile
     * @return
     */
    protected boolean checkLocalFile(String strLocalFile) {
        boolean result = false;
        File file = new File(strLocalFile);
        if (file.exists()) {
            result = true;
        }

        return result;
    }

    /**
     * @param strLocalFile
     */
    protected void createLocalFolder(String strLocalFile) {
        File fileOut = new File(strLocalFile);
        File fileParent = fileOut.getParentFile();
        if (!fileParent.exists()) {
            fileParent.mkdirs();
        }

        fileOut = null;
    }

    /**
     * @param fileAddress
     * @return
     */
    protected String getFileName(String fileAddress) {
        String fileName = StringUtils.substringAfterLast(fileAddress.replaceAll("\\\\", "/"), "/");
        return fileName;
    }

    /**
     * @param filePath
     * @return
     */
    protected String formatSeparator(String filePath) {
        return filePath;
    }

    /**
     * connection
     *
     * @return
     */
    protected abstract boolean connection();

    /**
     * disConnection
     */
    protected abstract void disConnection();

    /**
     * descriptions
     * @param var1
     * @param var2
     * @return
     */
    protected abstract boolean uploadFileSub(String var1, String var2);

    /**
     * descriptions
     * @param var1
     * @param var2
     * @return
     */
    protected abstract boolean downloadFileSub(String var1, String var2);

    /**
     * descriptions
     * @param var1
     * @return
     */
    protected abstract boolean deleteFileSub(String var1);

    /**
     * descriptions
     * @param var1
     * @return
     */
    protected abstract boolean createDirectorySub(String var1);

    /**
     * descriptions
     * @param var1
     * @return
     */
    protected abstract boolean removeDirectorySub(String var1);

    /**
     * descriptions
     * @param var1
     * @param var2
     * @return
     */
    protected abstract boolean copyFileSub(String var1, String var2);

    /**
     * descriptions
     * @param var1
     * @param var2
     * @return
     */
    protected abstract boolean renameSub(String var1, String var2);

    /**
     * descriptions
     * @param var1
     * @return
     */
    protected abstract List<String> getFileListSub(String var1);

    /**
     * descriptions
     * @param var1
     * @return
     */
    protected abstract boolean checkRemoteFileSub(String var1);

    /**
     * descriptions
     * descriptions
     * @param var1
     * @return
     */
    protected abstract boolean changeWorkingDirectorySub(String var1);
}
